"use strict";
const async = require('async');
const superagent = require('superagent');
const designVideoUniTest = require('./designVideoUniTest');
const authorizeUniTest = require('./authorizeUniTest');
const productsVideoUniTest = require('./productsVideoUniTest');
const retUtil = require('../routes/v1/utils/commonUtils/retUtil');


const uniTest = (function () {

    function uniTest() {
    }

    // 单元测试开始, 准备方法
    uniTest.testBegin = function () {

        let testDomain = 'http://test-openApi.yijianxiu.com';
        authorizeUniTest.setTestDomain(testDomain);
        designVideoUniTest.setTestDomain(testDomain);
        productsVideoUniTest.setTestDomain(testDomain);




        //正确参数
//        authorizeUniTest.AuthorizeTestCase1(function (err,data) {
//            console.log("authorizeUniTest 正确参数返回值  "+data+"  ,err = "+err);
//        });
//        //错误参数
//        authorizeUniTest.AuthorizeTestCase2(function (err,data) {
//            console.log("authorizeUniTest 错误参数返回值  错误信息:"+err+", data = "+data);
//        });
//        //访问过申请token接口后token会变更
//        designVideoUniTest.designVideoGetDesignContentTestCase1(function (err,data) {
//            console.log("designVideoUniTest 正确参数返回值  "+data+"  ,err = "+err);
//        });
//        designVideoUniTest.designVideoGetDesignContentTestCase2(function (err,data) {
//            console.log("designVideoUniTest 错误参数返回值  "+data+"  ,err = "+err);
//        });
//        designVideoUniTest.designVideoGetDesignListTestCase1(function (err,data) {
//            console.log("designVideoUniTest 正确参数返回值  "+data+"  ,err = "+err);
//        });
//        designVideoUniTest.designVideoGetDesignListTestCase2(function (err,data) {
//            console.log("designVideoUniTest 错误参数返回值  "+data+"  ,err = "+err);
//        });
//        designVideoUniTest.designVideoGetDesignContentTestCase3(function (err,data) {
//            console.log("designVideoUniTest design_id参数错误返回值  "+data+"  ,err = "+err);
//        });
//        productsVideoUniTest.productCrateVideoDocTestCase1(function (err,data) {
//            console.log("productsVideoUniTestCrateVideo 参数正确返回值  "+data+"  ,err = "+err);
//        });
//        productsVideoUniTest.productUpdateVideoDocDocTestCase1(function (err,data) {
//            console.log("productsVideoUniTestUpdateVideo 参数正确返回值  "+data+"  ,err = "+err);
//        });
//        productsVideoUniTest.productGetVideoDocContentTestCase1(function (err,data) {
//            console.log("productsVideoUniTestUpdateVideo 参数正确返回值  "+data+"  ,err = "+err);
//        });
//        productsVideoUniTest.productTypesetVideoTestCase1(function (err,data) {
//            console.log("productsVideoUniTestUpdateVideo 参数正确返回值  "+data+"  ,err = "+err);
//        });

        uniTest.execTestCase();
    };


    // 所有接口串行测试执行, 串行执行
    uniTest.execTestCase = function () {
        let testCases = [];
        // 默认 case, 准备参数
        testCases.push(function (callback) {
            let data = {};
            let sign = "75a21282497b3a3fa922fd15a29c04ca620eff9f";

            let param = {
                api_key:"YJX3SG7012EQ90VI",
                nonce_str :"fdsdsfdsf",
                expiration:1530288000,
                signature:sign.toUpperCase()
            };


            data.param = param;
            return callback(null, data);
        });
        //获取token
        testCases.push(function (data, callback) {
             authorizeUniTest.applyToken(data.param,function (err,result) {
                 if (err){
                     return callback(err, null);
                 }
                 data.token = result;
                 return callback(null,data);
             });
        });
         //查询token
         testCases.push(function (data, callback) {
             if (!data.token){
                 return callback("未获取到token",null);
             }
             let param = {
                 api_key : data.param.api_key,
                 token : data.token,
             };
             authorizeUniTest.queryToken(param, function (error, result) {
                 if (error){
                     return callback(error, null);
                 }
                 console.log('authorizeUniTest.queryToken: ' + JSON.stringify(result));
                 return callback(error, data);
             });
         });
         // 获取可用模板列表
         testCases.push(function (data, callback) {
             if (!data.token){
                 return callback("未获取到token",null);
             }
             let param = {
                 token : data.token,
                 page_num : 0,
                 page_size : 12
             };
             designVideoUniTest.getDesignList(param, function (error, result) {
                 if (error) {
                     return callback(error,null);
                 }
                 data.designList = result;
                 return callback(null, data);
             });
         });
         //获取单个模板详细信息
         testCases.push(function (data, callback) {
             if (!data.token){
                 return callback("未获取到token",null);
             }
             let param = {
                 token : data.token,
                 design_id : data.designList.list[0].design_id
             };
             designVideoUniTest.getDesignContent(param,function (error, result) {
                if (error){
                    return callback(error,null);
                }
                data.oneDesignInfo = result;
                return callback(error, data);
             })
         });
         //创建作品
         testCases.push(function (data, callback) {
             if (!data.token){
                 return callback("未获取到token",null);
             }
             let param = {
                 token : data.token,
                 design_id :  data.oneDesignInfo.design_id,
                 content : JSON.stringify(data.oneDesignInfo.content)
             };
             productsVideoUniTest.crateVideoDoc(param,function (err,result) {
                 if (err){
                     return callback(err);
                 }
                 data.product_id = result.product_id;
                 return callback(null,data);
             });
         });
        //修改作品
         testCases.push(function (data, callback) {
             if (!data.token){
                 return callback("未获取到token",null);
             }
             let param = {
                 token : data.token,
                 product_id :  data.product_id,
                 content : JSON.stringify(data.oneDesignInfo.content)
             };
             productsVideoUniTest.updateVideoDoc(param,function (err,result) {
                 if (err){
                     return callback(err);
                 }
                 console.log("productsVideoUniTest.updateVideoDoc ："+result);
                 return callback(null,data);
             });
         });
         //获取作品信息
         testCases.push(function (data, callback) {
             if (!data.token){
                 return callback("未获取到token",null);
             }
             let param = {
                 token : data.token,
                 product_id :  data.product_id,
             };
             productsVideoUniTest.getVideoDocContent(param,function (err,result) {
                 if (err){
                     return callback(err);
                 }
                 data.userVideo = result;
                 return callback(null,data);
             });
         });
        //获取视频作品预览
        testCases.push(function (data, callback) {
            if (!data.token){
                return callback("未获取到token",null);
            }
            let param = {
                token : data.token,
                product_id :  data.product_id,
            };
            productsVideoUniTest.typesetVideoDoc(param,function (err,result) {
                if (err){
                    return callback(err);
                }
                data.videoDocInfo = result;
                return callback(null,data);
            });
        });
        async.waterfall(testCases, function (error, data) {
            console.log('error: ' + error + ' data: ' + JSON.stringify(data || {}));
            uniTest.testEnd();
        });
    };

    // 单元测试执行 串行执行
    uniTest.execTestCase = function(){

        var testCases = [];

        // 申请token case
        testCases.push(function(callback){

            var au = new authorizeUniTest();
            au.AuthorizeTestCase1(function(error){
                var c = callback();
                console.log(c);
                return callback();
            })
        });



    }


    // 单元测试结束, 结束进程
    uniTest.testEnd = function () {
        process.exit();
    };

    return uniTest;
})();
uniTest.testBegin();
module.exports = uniTest;